<?php
namespace Admin\Controller;

class ManagerController extends BaseController{
    public function index(){
    	$Manager = D('Manager');
    	$this->assign('All',$Manager->all($this->page($Manager->total())));
    	$this->display();
    }
    
    public function add(){
    	if (IS_POST){
    		$Manager = D('Manager');
    		$object = $Manager->insert();
    		if (is_numeric($object)){
    			$object>0 ? $this->success(U(CONTROLLER_NAME.'/index'),'管理员添加成功！') : $this->failed('管理员添加失败！');
    		}else{
    			$this->failed($object);
    		}
    	}
    	$this->permit();
    	$this->display();
    }
    
    public function update(){
		if (I('get.id')){
			$Manager = D('Manager');
			if (IS_POST){
				//if (in_array(I('get.id'),array(1))) $this->failed('演示站，id为1的管理员无法修改！');
				$object = $Manager->update();
				is_numeric($object) ? $this->success(U(CONTROLLER_NAME.'/index'),'管理员修改成功！') : $this->failed($object);
			}
			$object = $Manager->one();
			if (!$object) $this->failed('不存在此管理员！');
			$this->permit($object['permit']);
			$this->assign('One',$object);
			$this->display();
		}else{
			$this->failed('非法操作！');
		}
    }
    
    public function activation(){
		if (I('get.id')){
			$Manager = D('Manager');
			$object = $Manager->one();
			if (!$object) $this->failed('不存在此管理员！');
			if ($object['level'] == 1) $this->failed('无法激活超级管理员！');
    		if ($object['level'] == 0){
    			if (!$Manager->activation(2)) $this->failed('管理员激活失败！');
    		}else{
    			if (!$Manager->activation(0)) $this->failed('管理员取消激活失败！');
    		}
    		$this->success(C('PREV_URL'));
    	}else{
    		$this->failed('非法操作！');
    	}
    }
    
    public function qq(){
		if (I('get.id')){
			$Manager = D('Manager');
			$object = $Manager->one();
			if (!$object) $this->failed('不存在此管理员！');
			if (!$Manager->qq('')) $this->failed('管理员QQ解除绑定失败！');
    		$this->success(C('PREV_URL'));
    	}else{
    		$this->failed('非法操作！');
    	}
    }
    
    public function delete(){
		if (I('get.id')){
			$Manager = D('Manager');
			if (IS_POST) $Manager->remove() ? $this->success(I('post.prev'),'管理员删除成功！') : $this->failed('管理员删除失败！');
			$object = $Manager->one();
			if (!$object) $this->failed('不存在此管理员！');
			if ($object['level'] == 1) $this->failed('无法删除超级管理员！');
    		$this->confirm('您真的要删除这条数据么？');
    	}else{
    		$this->failed('非法操作！');
    	}
    }
    
    private function permit($n=array()){
		$Permit = D('Permit');
		$selected = arrToStr($Permit->all4(),'id');
		$n = is_array($n) ? $selected : $n;
		$html = '';
		$object = $Permit->all2();
		if ($object){
			$html .= '<table>';
			foreach ($object as $value){
				$html .= '<tr>';
				$html .= '<td><label'.(in_array($value['id'],explode(',',$selected)) ? ' class="red"' : '').'><input type="checkbox" name="permit[]"'.(in_array($value['id'],explode(',',$n)) ? ' checked' : '').' value="'.$value['id'].'">'.$value['name'].'&nbsp;</label></td><td>';
				$object2 = $Permit->all3($value['id']);
				if ($object2){
					foreach ($object2 as $v){
						$html .= '<label class="'.(in_array($v['id'],explode(',',$selected)) ? ' red' : 'blue').'"><input type="checkbox" name="permit[]"'.(in_array($v['id'],explode(',',$n)) ? ' checked' : '').' value="'.$v['id'].'">'.$v['name'].'</label> ';
					}
				}
				$html .= '</td></tr>';
			}
			$html .= '</table>';
		}
		$this->assign('Permit',$html);
	}
}